03. Terms to Remember
Terms and Def
Core Terms
Now that we've seen some examples, we should take some time to define more important terms related to operators and expressions:
- Operand: a target or value affected by an operator
- In the expression, 8 - 5, the subtraction operator (-) is applied to (or “affects”) two operands: 8 and 5.
- Precedence: a rule or priority that defines when an operator should be applied in an expression; all operator precedence rules combine to form an “order of operations"
- In the expression, 6 + 5 * 2, operator precedence dictates that multiplication should be applied before addition. Therefore the expression equates to 6 + 10, and then to 16.
- Precedence Group: a collection of operators that share the same precedence
- Associativity: a property that determines the direction in which operators of the same precedence are applied; usually individual operators are specified as left-associative or right-associative
- For example, addition is left-associative. This means the following expression would be evaluated as follows:
- 5 + 4 + 2
- ((5 + 4) + 2)
- (9 + 2)
- 11
- Conversely, exponentiation in math is right-associative. This means the following expression would be evaluated as follows…
- 5 ^ 2 ^ 2
- (5 ^ (2 ^ 2))
- (5 ^ 4)
- 625
- Note: There is no built-in exponential operator (^) in Swift.
- For example, addition is left-associative. This means the following expression would be evaluated as follows:
Unary Operators
Unary Operators
The next group of terms you should know are related to unary operators:
- **Unary operator **- an operator that is applied to a single operand.
- For example, unary minus (-) is a unary operator. Here is an expression where it is being applied to a single operand, points:
- -points
- For example, unary minus (-) is a unary operator. Here is an expression where it is being applied to a single operand, points:
- Prefix notation - a designation for when an unary operator appears immediately before its operand.
- In the following expression, the unary minus operator (-) uses prefix notation:
- -points
- In the following expression, the unary minus operator (-) uses prefix notation:
- Postfix notation - a designation for when an unary operator appears immediately after its operand.
- In the following expression, the forced unwrapped operator (!) uses postfix notation:
- optionalValue!
- NOTE: We will explain how the forced unwrapped operator works in a later session on Optionals.
- In the following expression, the forced unwrapped operator (!) uses postfix notation:
Binary Operators
Binary Operators
One step above unary operators are binary operators, and here are the related terms:
- Binary operator: an operator that is applied to two operands.
- Most of your basic arithmetic operators—like addition (+), subtraction (-), multiplication (*), and division (/)—are binary operators. Here is an example using the addition operator (+). It is being applied to the
salesPriceandsalesTaxoperands:salesPrice + salesTax
- Most of your basic arithmetic operators—like addition (+), subtraction (-), multiplication (*), and division (/)—are binary operators. Here is an example using the addition operator (+). It is being applied to the
- Infix notation: a designation for when an operator appears between two operands.
- In the following expression, the addition operator (+) uses infix notation:
- 2 + 3
- In the following expression, the addition operator (+) uses infix notation:
Ternary Operators
Ternary Operators
And one more step above binary operators are ternary operators:
- **Ternary operator **: an operator that is applied to three operands
Ternary operators are much less common than binary operators, but one of the most common ternary operators is the ternary conditional operator. It is used to choose between two values based on a truth value, and it uses the following syntax:
trueOrFalseValue ? useThisIfTrue : useThisIfFalse
Let's use a concrete example to demonstrate. Assume we want to calculate the total cost of an item in a store. The item can optionally have tax applied. If the item is tax free, then the total cost is the same as the price without any sales tax. If the item has sales tax, then the total cost is the price plus sales tax. We can easily represent this with a ternary conditional operator:
isItemTaxFree ? priceWithoutTax : priceWithTax
To evaluate this statement, the ternary conditional operator looks at the value before the question mark: isItemTaxFree. If the value is true (i.e. the item is tax free), then the ternary conditional operator returns the value before the colon: priceWithoutTax. On the other hand, if the value is false (i.e. the item is not tax free and requires sales tax), then the ternary conditional operator returns the value after the colon—priceWithTax.
Standard Operators in Swift
Standard Operators in Swift
Below, we have created a table of some of the standard operators in Swift. Don’t worry if you do not understand the meaning of each operator yet because we will incorporate them more and more.
Here is an ordering of the precedence groups listed below from highest to lowest:
- Multiplication
- Addition
- Assignment
Operators with a higher precedence group will be calculated before operators with a lower precedence group.
| Operator | Name | Description | Precedence Group | Associativity | Type |
|---|---|---|---|---|---|
| * | Multiply | Performs basic multiplication | Multiplication | Left | Binary, Infix |
| / | Divide | Performs basic division (there are special rules governing division of integers and decimal values) | Multiplication | Left | Binary, Infix |
| % | Remainder | Performs basic division and returns the remainder that is left over | Multiplication | Left | Binary, Infix |
| + | Add | Performs basic addition | Addition | Left | Binary, Infix |
| - | Subtract | Performs basic subtraction | Addition | Left | Binary, Infix |
| = | Assignment | Assigns a value to a variable | Assignment | Right | Binary, Infix |
| += | Add and assign | Performs + and then assigns the result to a variable | Assignment | Right | Binary, Infix |
| -= | Subtract and assign | Performs - and then assigns the result to a variable | Assignment | Right | Binary, Infix |
| *= | Multiply and assign | Performs * and then assigns the result to a variable | Assignment | Right | Binary, Infix |
| /= | Divide and assign | Performs / and then assigns the result to a variable | Assignment | Right | Binary, Infix |
| %= | Remainder and assign | Performs % and then assigns the result to a variable | Assignment | Right | Binary, Infix |